/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package servlets;

import bean.GeolocalizacionBean;
import bean.LogueoBean;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.URL;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;
import org.netbeans.saas.RestResponse;
import org.netbeans.saas.google.GoogleMapService;

/**
 *
 * @author Ing. Monica Merlo, Ing. Agustin  Fernandez
 */
public class ConsultaGeocodingServlet extends HttpServlet {

    static Logger logger = Logger.getLogger(ConsultaGeocodingServlet.class);

    public static void log4jArchivo (Exception e){
        /**
         * Primero obtenemos la url del archivo de configuración,
         * como están en el mismo paquete que la clase Main lo podemos obtener con el método getResource().
         */
        URL url = GeocodingServlet.class.getResource("/configuracion/log4j.properties");
        /**
         * Despues cargamos el archivo de configuración
         */
        PropertyConfigurator.configure(url);
        logger.error(e);
    }

    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            try {
                LogueoBean logueoUsuario = (LogueoBean) request.getSession().getAttribute("LogueoBean");
                GeolocalizacionBean geo = (GeolocalizacionBean) request.getSession().getAttribute("GeolocalizacionBean");
                if(geo.getPais().equals("")){
                    geo.setPais("###");
                }
                RestResponse result = GoogleMapService.getGoogleMapsConsultaGeo(logueoUsuario, request.getContextPath(), geo, geo.getPais(), geo.getObrasFiltradasConsultaGeo(), 5);
                out.println(result.getDataAsString());

            } catch (Exception ex) {
                ConsultaGeocodingServlet.log4jArchivo(ex);
                out.println("<html>");
                out.println("<head>");
                out.println("<title>Servlet GeocodingServlet</title>");
                out.println("</head>");
                out.println("<body bgcolor='white'>");
                out.println("<h2>Verifique su conexi&oacute;n a Internet para visualizar el mapa</h2>");
                out.println("</body>");
                out.println("</html>");
            }
        } finally {
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    } 

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
